<template>
	<view>
		<view style="padding-top: 40rpx">
			<radio-group @change="radioFun">
				<block v-if="list.length">
					<view class="address" v-for="(item,index) in list" :key="item.ID" @click="setDefault(item)">
						<view class="address-1">
							<view class="name">
								<text>{{item.Accepter}}</text>
								<text>{{item.Phone}}</text>
							</view>
							<view class="address-2">{{item.Address}}</view>
							<view class="use">
								<view class="use-1">
									<radio color="#706EEE" :vlaue="item.ID" :checked="item.IsDefault"></radio>
									<text>设为默认地址</text>
								</view>
								<view class="use-2">
									<view @tap.stop="AddressDel(item.ID)" class="del">删除</view>
									<view @tap.stop="openAddress(1,item)" style="padding-right: 0">编辑</view>
								</view>
							</view>
						</view>
					</view>
				</block>
				<view class="null" v-else>
					暂无收货地址
				</view>
			</radio-group>
			<view style="height: 178rpx"></view>
		</view>
		<view class="addbox">
			<view class="add" @tap="openAddress(0)">添加收货地址</view>
		</view>
		<uni-popup type="bottom" ref="pay" @maskClick="closeAddress">
			<view class="store">
				<view @click="closeAddress" class="staff_card_cont">
					<uni-icons type="closeempty" size="24" style="opacity: 0"></uni-icons>
					{{type==0?'添加':'编辑'}}收货地址
					<uni-icons type="closeempty" size="24"></uni-icons>
				</view>
				<view class="staff_card_cont">
					<view>收货人</view>
					<input type="text" v-model="params.Accepter" placeholder="请输入收货人名称" />
				</view>
				<view class="staff_card_cont">
					<view>手机号</view>
					<input type="tel" v-model="params.Phone" placeholder="请输入联系电话" maxlength="11"/>
				</view>
				<view class="staff_card_cont" @click="checkAddress">
					<view>收货地址</view>
					<view class="content">
						<view class="content_text" :class="params.Address?'':'gery'">{{params.Address || '请选择收货地址'}}</view>
						<uni-icons type="right" size="22"></uni-icons>
					</view>
				</view>
				<view class="staff_card_cont">
					<view>详细地址(选填)</view>
					<input type="text" v-model="house" placeholder="如:大厦A座101室" />
				</view>
				<view class="staff_card_cont">
					<view>设为默认地址</view>
					<view class="content">
						<u-radio-group v-model="params.IsDefault">
							<u-radio activeColor="#706EEE" :name="1" shape="circle" labelColor="#111" labelSize='15'
								label="默认"></u-radio>
							<view style="width: 20rpx;"></view>
							<u-radio activeColor="#706EEE" :name="0" shape="circle" labelColor="#111" labelSize='15'
								label="不设置"></u-radio>
						</u-radio-group>
					</view>
				</view>
				<view class="cont_sub" v-if="type==0" @click="AddressAdd">确定</view>
				<view class="cont_sub" v-else @click="AddressEdit">确定</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import address from './address.js';
	export default {
		data() {
			return {
				house: '',
				params: {
					Province: '', // 省份
					City: '', // 城市
					Area: '', // 区
					Address: '', // 详细地址
					Accepter: '', // 收货人
					Phone: '', // 联系电话
					IsDefault: 0, // 是否默认收货地址
				},
				list: [],
				type: 0
			};
		},
		onLoad() {
			this.init();
		},
		methods: {
			init() {
				this.MyAddress();
				this.MyDefaultAddress();
			},
			radioFun(e) {
				console.log(e);
			},
			async MyAddress() {
				let res = await this.$api.post('/UserAddress/MyAddress');
				this.list = res.data
			},
			async MyDefaultAddress() {
				let res = await this.$api.post('/UserAddress/MyDefaultAddress');
			},
			closeAddress() {
				Object.keys(this.params).forEach(key => {
					this.params[key] = key == 'IsDefault' ? 0 : ''
				})
				this.$refs.pay.close()
			},
			openAddress(i, item) {
				if (i == 1) {
					this.params = JSON.parse(JSON.stringify(item))
					delete this.params.UserID
				}
				this.type = i
				this.$refs.pay.open()
			},
			async setDefault(item) {
				if (item.IsDefault == 1) return
				let res = await this.$api.post('/UserAddress/SetDefault', { ID: item.ID });
				this.$pv.msg(res.code == 0 ? '设置成功' : res.message)
				this.MyAddress()
			},
			async AddressAdd() {
				this.type = 0
				let con = this.params
				if (!con.Accepter) return this.$pv.msg('请输入收货人名称')
				if (!con.Phone) return this.$pv.msg('请输入手机号')
				if (!con.Address) return this.$pv.msg('请选择收货地址')
				con.Address += this.house

				let res = await this.$api.post('/UserAddress/Add', this.params);
				this.$pv.msg(res.code == 0 ? '添加完成' : res.message)

				this.MyAddress()
				this.closeAddress()
			},
			async AddressEdit() {
				this.type = 1
				let con = this.params
				if (!con.Accepter) return this.$pv.msg('请输入收货人名称')
				if (!con.Phone) return this.$pv.msg('请输入手机号')
				if (!con.Address) return this.$pv.msg('请选择收货地址')
				con.Address += this.house

				let res = await this.$api.post('/UserAddress/Edit', this.params);
				this.$pv.msg(res.code == 0 ? '保存完成' : res.message)

				this.MyAddress()
				this.closeAddress()
			},
			AddressDel(ID) {
				let _this = this
				uni.showModal({
					title: "温馨提示",
					content: "确定要删除地址吗？",
					confirmText: "确认",
					success: async function (res) {
						if (res.confirm) {
						let res1 = await _this.$api.post('/UserAddress/Delete', { ID });
						_this.$pv.msg(res1.code == 0 ? '删除完成' : res.message)
						_this.MyAddress()
						} else if (res.cancel) {

						}
					},
				});

			},

			checkAddress() {
				uni.chooseLocation({
					success: (res) => {
						var reg = /.+?(省|市|自治区|自治州|县|区)/g;
						let myLocation = res.address.match(reg);
						let provinceName = '';
						let cityName = '';
						let countryName = '';
						if (myLocation != undefined && myLocation != null) {
							provinceName = myLocation[0]; //省
							cityName = myLocation[1]; //市
							countryName = myLocation[2]; //区
							if (myLocation.length == 2) {
								provinceName = myLocation[0]; //省
								cityName = myLocation[0]; //市
								countryName = myLocation[1]; //区
							}
						}
						this.params.Province = provinceName;
						this.params.City = cityName;
						this.params.Area = countryName
						this.params.Address = res.address;
						if (countryName == undefined) {
							this.params.Province = provinceName;
							this.params.City = provinceName;
						}
						console.log(res);
					},
				});
			},
		},
	};
</script>

<style lang="scss">
	.gery {
		color: #a8a8a8 !important;
	}

	.null {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		color: #a8a8a8;
		height: 50vh;
	}

	.input-placeholder {
		font-size: 28rpx;
	}

	.address {
		margin: 40rpx 36rpx 0;
		background: #f7f7f7;
		border-radius: 14rpx;
		padding: 30rpx 36rpx 25rpx;
		display: flex;

		&:first-child {
			margin-top: 0;
		}

		.img {
			margin-top: 26rpx;
			margin-right: 26rpx;
			width: 58rpx;
			height: 58rpx;
			flex-shrink: 0;
		}

		.address-1 {
			flex: 1;

			.name {
				display: flex;
				align-items: center;

				text {
					&:first-child {
						color: #000000;
						font-size: 28rpx;
						line-height: 33rpx;
					}

					&:last-child {
						margin-left: 12rpx;
						color: #000000;
						font-size: 24rpx;
						line-height: 28rpx;
					}
				}
			}

			.address-2 {
				margin-top: 13rpx;
				color: #7f828d;
				font-size: 24rpx;
				line-height: 1.4;
				padding-bottom: 26rpx;
				border-bottom: 1rpx solid #c8cbd8;
			}

			.use {
				margin-top: 8rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.use-1 {
					display: flex;
					align-items: center;

					image {
						width: 52rpx;
						height: 52rpx;
					}

					text {
						font-size: 24rpx;
						color: #7f828d;
						line-height: 23rpx;
					}
				}

				.use-2 {
					display: flex;
					align-items: center;

					text {
						font-size: 20rpx;
						line-height: 23rpx;
						color: #181818;
						margin-left: 30rpx;
					}

					view {
						font-size: 26rpx;
						padding: 10rpx 16rpx;
						color: #111;
					}

					.del {
						color: #e71b00;
					}

					.edit {}
				}
			}
		}
	}

	.addbox {
		width: 100%;
		position: fixed;
		bottom: 0;
		left: 0;
		background-color: #fff;
		box-sizing: border-box;
		padding: 24rpx 36rpx;
		padding-bottom: 40rpx;

		.add {
			height: 88rpx;
			line-height: 88rpx;
			border-radius: 110rpx;
			background-color: #706EEE;
			text-align: center;
			color: #fff;
			font-size: 32rpx;
			font-weight: bold;
			letter-spacing: 3px;
		}
	}

	.cancel {
		width: 540rpx;
		background: #eeeff3;
		border-radius: 30rpx 30rpx 30rpx 30rpx;
		overflow: hidden;

		.title {
			margin-top: 32rpx;
			color: #181818;
			line-height: 42rpx;
			text-align: center;
			font-size: 40rpx;
		}

		.cancel-0 {
			display: flex;
			align-items: center;
			padding: 38rpx 36rpx;
			justify-content: space-between;

			.cancel-1 {
				width: 225rpx;
				height: 70rpx;
				background: #eeeff3;
				box-shadow: 10rpx 10rpx 20rpx 0rpx #bfc2cd, -10rpx -10rpx 20rpx 0rpx #ffffff, inset 10rpx 10rpx 50rpx 0rpx #ced5e1,
					inset -10rpx -10rpx 50rpx 0rpx #ffffff;
				border-radius: 12rpx;
				color: #181818;
				font-size: 28rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.cancel-2 {
				margin-left: 18rpx;
				width: 225rpx;
				height: 70rpx;
				background: #eeeff3;
				box-shadow: 10rpx 10rpx 20rpx 0rpx #bfc2cd, -10rpx -10rpx 20rpx 0rpx #ffffff, inset 10rpx 10rpx 50rpx 0rpx #ced5e1,
					inset -10rpx -10rpx 50rpx 0rpx #ffffff;
				border-radius: 12rpx;
				color: #3fc4b6;
				font-size: 28rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}

	.currency {
		display: flex;
		align-items: center;
		padding: 30rpx 40rpx;
		border-bottom: 1rpx solid #c8cbd8;

		text {
			width: 96rpx;
			color: #181818;
			font-size: 24rpx;
			line-height: 28rpx;
		}

		input {
			width: 352rpx;
			margin-left: 35rpx;
			color: #181818;
			font-size: 24rpx;
			line-height: 28rpx;
		}
	}

	.currency-1 {
		display: flex;
		align-items: center;
		padding: 30rpx 40rpx;
		border-bottom: 1rpx solid #c8cbd8;

		.currency-1-1 {
			width: 96rpx;
			color: #181818;
			font-size: 24rpx;
			line-height: 28rpx;
		}

		.currency-1-2 {
			margin-left: 35rpx;
			width: 352rpx;
			color: #7f828d;
			font-size: 24rpx;
			line-height: 28rpx;

			&.currency_1_2 {
				color: #181818;
			}
		}

		.currency-1-3 {
			color: #181818;
		}

		image {
			margin-left: 20rpx;
			width: 32rpx;
			height: 32rpx;
		}
	}

	.currency-2 {
		display: flex;
		padding: 30rpx 40rpx;
		border-bottom: 1rpx solid #c8cbd8;

		text {
			width: 96rpx;
			color: #181818;
			font-size: 24rpx;
			line-height: 28rpx;
		}

		textarea {
			width: 352rpx;
			margin-left: 35rpx;
			color: #181818;
			font-size: 24rpx;
			line-height: 28rpx;
			height: 64rpx;
		}
	}

	/deep/ .dialog-caption,
	/deep/ .picker-view {
		background: #eeeff3;
	}

	/deep/ .selected-item-active,
	/deep/ .check {
		border-color: #3fc4b6 !important;
	}

	/deep/ .item {
		border-bottom: 1px solid #c8cbd8;
	}

	.addressBox {
		width: 90vw;
		padding: 24rpx;
		box-sizing: border-box;
		height: 60vh;
		background-color: #fff;
		border-radius: 24rpx;

		.add_tit {
			position: sticky;
			top: 0;
			left: 0;
			text-align: center;
		}
	}

	.store {
		background-color: white;
		padding-bottom: 40rpx;
		width: 750rpx;
		position: fixed;
		z-index: 20rpx;
		bottom: 0;
		border-radius: 32rpx 32rpx 0 0;

		.staff_card_cont {
			view {
				color: #333;
			}

			display: flex;
			justify-content: space-between;
			border-bottom: 8rpx solid #f7f7f7;
			font-size: 28rpx;
			align-items: center;
			color: #181818;
			padding: 0rpx 40rpx;

			text {
				color: #000;
				font-size: 36rpx;
			}

			input {
				text-align: right;
				padding: 24rpx;
			}

			.content {
				padding: 24rpx 0;
				max-width: 75%;
				display: flex;
				align-items: center;

				.content_text {
					flex: 1;
				}
			}

			.countbox {
				display: flex;
				align-items: center;
				justify-content: flex-end;

				input {
					width: 15%;
					text-align: center;
					border: 2rpx solid #eee;
					border-radius: 8rpx;
					padding: 10rpx;
					margin: 0 10rpx;
				}
			}
		}

		.staff_card_cont:first-child {
			justify-content: space-between;
			margin-right: -8rpx;
			font-size: 30rpx;
			padding: 24rpx 40rpx;
		}

		.cont_sub {
			display: flex;
			justify-content: center;
			align-items: center;
			background-color: #706EEE;
			margin: 0 40rpx;
			margin-top: 20rpx;
			padding: 20rpx 0;
			border-radius: 50rpx;
			color: white;
		}
	}
</style>